package com.omni.monitor.plugin.threadpool;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration
public class ThreadPoolAutoScaleConfig {

    /**
     * 配置线程池自动扩展器，添加订单处理线程池的时段调整规则
     */
    //@Bean(initMethod = "start", destroyMethod = "stop")
    public ThreadPoolAutoScale threadPoolAutoScale(ThreadPoolMonitor monitor) {
        return new ThreadPoolAutoScale(monitor)
                .addRule("订单处理线程池", pool -> {
                    // 根据当前小时动态调整核心线程数
                    int hourOfDay = LocalDateTime.now().getHour();
                    int baseSize = 1;

                    // 高峰期（12-18点）：基础大小的3倍
                    if (hourOfDay >= 12 && hourOfDay <= 18) {
                        return baseSize * 3;
                    }
                    // 次高峰期（9-12点、18-20点）：基础大小的2倍
                    else if ((hourOfDay >= 9 && hourOfDay < 12) ||
                            (hourOfDay > 18 && hourOfDay <= 20)) {
                        return baseSize * 2;
                    }
                    // 普通时段：基础大小
                    else {
                        return baseSize;
                    }
                })
                // 可添加更多线程池的规则
                .addRule("支付处理线程池", pool -> {
                    // 示例：根据队列大小动态调整
                    if (pool.getQueueSize() > 50) {
                        return Math.min(pool.getCorePoolSize() + 5, pool.getMaximumPoolSize());
                    } else {
                        return Math.max(pool.getCorePoolSize() - 2, 5);
                    }
                });
    }
}